package com.fengli.task.service.serviceImpl;

import com.fengli.task.dao.TaskMapper;
import com.fengli.task.dao.WeekSummaryMapper;
import com.fengli.task.entity.TaskEntity;
import com.fengli.task.entity.WeekSummaryEntity;
import com.fengli.task.uitl.Result;
import com.fengli.task.uitl.enumType.Code;
import com.fengli.task.uitl.exception.InsertDataToDatabaseException;
import com.fengli.task.uitl.exception.SelectDataFromDatabaseException;
import com.fengli.task.uitl.exception.UpdateDataToDatabaseException;
import javafx.concurrent.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TaskServiceImpl implements com.fengli.task.service.TaskService {

    @Autowired
    //进行数据库访问
    private TaskMapper taskDao;
    @Autowired
    // 对week_summary表进行查询
    private WeekSummaryMapper weekSummaryDao;

    @Override
    public void addTask(TaskEntity taskEntity) {
        //调用service层
        try {
            taskDao.insert(taskEntity);
        }catch (Exception e){
            throw new InsertDataToDatabaseException("插入任务到数据库时发生了错误！");
        }
    }

    @Override
    public TaskEntity queryTask(String taskId) {
        try{
            return taskDao.queryTaskById(taskId);
        }catch (Exception e){
            throw new SelectDataFromDatabaseException("查询任务失败");
        }

    }

    @Override
    public List<TaskEntity> queryTaskList(TaskEntity taskCondition) {
        try{
            return taskDao.queryTaskList(taskCondition);
        } catch (Exception e) {
            throw new SelectDataFromDatabaseException("查询任务列表失败");
        }
    }

    @Transactional
    @Override
    public Result queryData(String userId) {
        Result result = new Result();
        Object[] data = new Object[2];
        /*封装查询条件*/
        TaskEntity condition = new TaskEntity();
        condition.setUserId(userId);
        condition.setDel(false);
        // 获取任务列表
        data[0]=queryTaskList(condition);
        /* 获取周总结 */
        WeekSummaryEntity weekSummaryCondition = new WeekSummaryEntity();
        weekSummaryCondition.setUserId(userId);
        try{
            data[1]=weekSummaryDao.queryWeekSummaryList(weekSummaryCondition);
        }catch (Exception e){
            throw new SelectDataFromDatabaseException("查询周总结列表失败");
        }
        result.setData(data);
        result.setCode(Code.SUCCESS);
        result.setMsg("获取task列表、周总结列表成功");
        return result;
    }

    @Override
    public void modifyTask(TaskEntity taskEntity) {
        try{
            taskDao.update(taskEntity);
        }catch (Exception e){
            throw new UpdateDataToDatabaseException("更新任务到数据库时发生了错误！");
        }
    }

    @Override
    public void saveTask(TaskEntity taskEntity) {
        // todo 先查询任务，如果没有该任务就是添加任务
        TaskEntity entity = queryTask(taskEntity.getId());
        if(entity == null){
            addTask(taskEntity);
        }else{
            modifyTask(taskEntity);
        }
    }
}
